package cn.bigfire.wzdj.net;

import cn.bigfire.wzdj.pojo.bo.WZDJPack;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;

import java.util.List;

/**
 * 业务层 编解码器
 */
public class WZDJPackWSEncoder extends MessageToMessageEncoder<WZDJPack> {


    @Override
    protected void encode(ChannelHandlerContext ctx, WZDJPack wzdjPack, List<Object> out) throws Exception {
        try {
            byte[] bytes = wzdjPack.getBytes();
//            System.out.printf("->cmd:%s,type:%s,len:%s[%s]%n", wzdjPack.getHeader().getId(),
//                    wzdjPack.getHeader().getName(), bytes.length, ByteUtil.getSize(bytes.length));
            ByteBuf outBuf = Unpooled.buffer();
            outBuf.writeBytes(bytes);

            BinaryWebSocketFrame binaryWebSocketFrame = new BinaryWebSocketFrame(outBuf);
            out.add(binaryWebSocketFrame);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
